package com.geekchique.runningbuddy.model;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import android.location.Location;

import com.geekchique.runningbuddy.util.DBHelperImpl;

public class Jog {
	public long _id;
	public long routeId;
	public String name;
	public long startTime;
	public long stopTime;
	public Calendar startTimeCal;
	public Calendar stopTimeCal;
	public String route;
	private Map<Long, Location> locationsAndTimes;
	private DBHelperImpl dbHelper;

	private boolean syncDatabase = false;

	public Jog() {
	}

	public Jog(long _id, String name, long startTime, long stopTime) {
		this._id = _id;
		this.name = name;
		this.startTime = startTime;
		this.stopTime = stopTime;

	}

	public Jog(String name, long startTime, long stopTime) {
		this.name = name;
		this.startTime = startTime;
		this.stopTime = stopTime;
		locationsAndTimes = new TreeMap<Long, Location>();
	}

	public Jog(String name, String route, TreeMap<Long, Location> locationsAndTimes) {
		this.name = name;
		this.route = route;
		this.locationsAndTimes = locationsAndTimes;
		this.startTime = locationsAndTimes.firstKey();
		this.stopTime = locationsAndTimes.lastKey();
	}

	// This is the preferred constructor when starting a jog. It creates the
	// entries in the database.
	public Jog(DBHelperImpl dbHelper, String name, long startTime) {
		this.name = name;
		this.startTime = startTime;
		this.createInDatabase(dbHelper);
		this.locationsAndTimes = new TreeMap<Long, Location>();
		syncDatabase = true;
	}

	public long getStopTime() {
		return this.stopTime;
	}

	public long get_id() {
		return _id;
	}

	public void set_id(long _id) {
		this._id = _id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getStartTime() {
		return this.startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public Calendar getStartTimeCal() {
		if (startTimeCal == null) {
			Calendar cal = new GregorianCalendar();
			cal.setTimeInMillis(startTime);
			return startTimeCal = cal;
		}
		return startTimeCal;
	}

	public void setStartTimeCal(Calendar startTimeCal) {
		this.startTimeCal = startTimeCal;
	}

	public Calendar getStopTimeCal() {
		if (stopTimeCal == null) {
			Calendar cal = new GregorianCalendar();
			cal.setTimeInMillis(stopTime);
			return stopTimeCal = cal;
		}
		return stopTimeCal;
	}

	public void setStopTimeCal(Calendar stopTimeCal) {
		this.stopTimeCal = stopTimeCal;
	}

	public void setStopTime(long stopTime) {
		this.stopTime = stopTime;
		if (syncDatabase) {

		}
	}

	public Map<Long, Location> getlocationsAndTimes() {
		return this.locationsAndTimes;
	}

	public double getDistance() {
		if (locationsAndTimes == null)
			return 0.0;

		double distance = 0.0;

		Location loc = null;

		Iterator<Map.Entry<Long, Location>> it = locationsAndTimes.entrySet()
				.iterator();
		while (it.hasNext()) {
			Location nextLoc = it.next().getValue();
			if (loc != null) {
				distance += loc.distanceTo(nextLoc);
			}

			loc = nextLoc;

		}

		return distance;

	}

	private void createInDatabase(DBHelperImpl db) {
		dbHelper = db;
		this._id = dbHelper.getNewJogId(this.name);
		this.routeId = dbHelper.getNewRouteId(this._id);
	}

	public void addRoutePoint(Location point) {
		locationsAndTimes.put(point.getTime(), point);
		if (syncDatabase) {
			dbHelper.insertRoutePoint(_id, routeId,
					locationsAndTimes.size() - 1, point);
		}
	}
}
